package com.iwith.dongmoon.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.iwith.dongmoon.util.SessionManager;

public class MainIconView extends Activity implements OnClickListener {
	private SessionManager mSessionManager=null;
//	private GridView mainIcon;
	private Button backbt;
	private Button btn_signin;
	private Button btn_list;
	private Button btn_news;
	
	private Context mContext;
	//private Integer[] iconResource = { R.drawable.tab_search, R.drawable.tab_news, R.drawable.tab_group, R.drawable.tab_favorite, R.drawable.tab_profile, R.drawable.logo, R.drawable.logo, R.drawable.logo, R.drawable.logo };
	//private String[] iconName = {"동문 목록", "소식란", "소모임", "관심 동문", "개인정보", "준비중..", "준비중..", "준비중..", "준비중.."};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.mainiconview);
	    // TODO Auto-generated method stub
	    mSessionManager=new SessionManager(this);
		
	    if(getIntent().getExtras()==null){
	    	if(mSessionManager.isLogOn()==false) {
	    		mSessionManager.login();
	    		//Intent intent = new Intent(this, SignUpWelcomeActivity.class);
	    		//startActivity(intent);
	    	}
		}
	}
	
	@Override
	public void onResume(){
		super.onResume();
		Initialize();
	}
	
	@Override
	public void onDestroy(){
		if(getIntent().getExtras()==null)
			mSessionManager.setIsLogOn(false);
		super.onDestroy();
	}
	
	private void Initialize(){
		mContext = this;
		
		if(mSessionManager.isLogOn()==false){
			Intent intent=new Intent(this, LoginActivity.class);
			startActivity(intent);
		}
		
//		List<MainIconItem> list=new ArrayList<MainIconItem>();
//		for (int i=0; i<iconName.length; i++){
//			MainIconItem mainIconItem=new MainIconItem(iconResource[i], iconName[i]);
//			if(getIntent().getExtras()==null){
//				mainIconItem.setFlag(false);
//			} else {
//				mainIconItem.setFlag(true);
//			}
//			
//			list.add(mainIconItem);
//		}
		
//		mainIcon=(GridView)findViewById(R.id.mainiconview_gridview);
		backbt=(Button)findViewById(R.id.backbtn);
		btn_signin=(Button)findViewById(R.id.mainiconview_btn_singin);
		btn_news=(Button)findViewById(R.id.mainiconview_btn_news);
		btn_list=(Button)findViewById(R.id.mainiconview_btn_list);
		
		btn_signin.setOnClickListener(this);
		btn_news.setOnClickListener(this);
		btn_list.setOnClickListener(this);
		
//		MainIconGridAdapter gridAdapter = new MainIconGridAdapter(this, R.id.mainiconview_gridview, (ArrayList<MainIconItem>)list);
//		mainIcon.setAdapter(gridAdapter);
		if(getIntent().getExtras()==null){
			backbt.setVisibility(View.GONE);
		} else {
			backbt.setVisibility(View.VISIBLE);
			backbt.setOnClickListener(this);
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==backbt.getId()){
			finish();
		}
		else if(v.equals(btn_signin) == true)
		{
			Intent intent=new Intent(mContext, SignUpDocumentActivity.class);
			mContext.startActivity(intent);
		}
		else if(v.equals(btn_news) == true)
		{
			Intent intent=new Intent(mContext, MainTabView.class);
			intent.putExtra("tab_position", 1);
			startActivity(intent);
		}
		else if(v.equals(btn_list) == true)
		{
			Intent intent=new Intent(mContext, MainTabView.class);
			intent.putExtra("tab_position", 0);
			//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			mContext.startActivity(intent);
		}
	}
}
